package vos.vn.custom;

import vos.vn.R;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class CustomSwitch extends ImageView implements OnClickListener  {
	private float dx;
	private float dy;
	private boolean on;
	
	public float getDx() {
		return dx;
	}

	public void setDx(float dx) {
		this.dx = dx;
	}

	public float getDy() {
		return dy;
	}

	public void setDy(float dy) {
		this.dy = dy;
	}

	public boolean isOn() {
		return on;
	}

	public void setOn(boolean on) {
		this.on = on;
	}
	
	public CustomSwitch(Context context) {
		super(context);
		setOnClickListener(this);
		if(on) {
			changeImage(R.drawable.switch_on);
		} else {
			changeImage(R.drawable.switch_off);
		}
	}
	
	public CustomSwitch(Context context, AttributeSet attrs) {
		super(context, attrs);
		setOnClickListener(this);
		if(on) {
			changeImage(R.drawable.switch_on);
		} else {
			changeImage(R.drawable.switch_off);
		}
	}
	
	public CustomSwitch(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);	
		setOnClickListener(this);
		if(on) {
			changeImage(R.drawable.switch_on);
		} else {
			changeImage(R.drawable.switch_off);
		}
	}

	public void translate(float dx, float dy) {
		this.dx = dx;
		this.dy = dy;
	}
	
	public void changeImage(int resId) {
		this.setImageResource(resId);
	}
	
	@Override
	protected void onDraw(Canvas canvas) 
	{
		canvas.translate(dx, dy);		
	    super.onDraw(canvas);
	    invalidate();
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
	    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	    int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
	    int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
	    
	    this.setMeasuredDimension(
	            parentWidth, parentHeight);
	}

	public boolean setFrameExt(int l, int t, int r, int b) {
		return setFrame(l, t, r, b);
	}

	@Override
	public void onClick(View v) {
		if(this.on) {
			changeImage(R.drawable.switch_off);
			setOn(false);
		} else {
			changeImage(R.drawable.switch_on);
			setOn(true);
		}
		
	}
}
